/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.skyeye.modeling.customerized.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.Polyline;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Font;
import org.skyeye.modeling.Activator;
import org.skyeye.modeling.customerized.model.SimpleInput;

/**
 * @author danlee
 */
public class BulbInputFigure extends InputFigure {

	private static final Dimension SIZE = new Dimension(50, 50);
	private Label nameLabel, numLabel;
	private Font font = new Font(Activator.getActiveWorkbenchShell().getDisplay(), "courier", 10, 1);

	public BulbInputFigure(String ID) {

		super();
		this.nameLabel = new Label();
		nameLabel.setText("LED" + ID);
		nameLabel.setTextAlignment(Label.LEFT);
		nameLabel.setIconTextGap(5);
		this.add(nameLabel);

		this.numLabel = new Label();
		// numLabel.setText("5V");
		// numLabel.setTextAlignment(Label.LEFT);
		// numLabel.setFont(font);;
		this.add(numLabel);
	}

	/**
	 * @see org.eclipse.draw2d.Figure#getPreferredSize(int, int)
	 */
	public Dimension getPreferredSize(int wHint, int hHint) {
		return SIZE;
	}

	/**
	 * @see org.eclipse.draw2d.Figure#paintFigure(Graphics)
	 */
	protected void paintFigure(Graphics g) {
		test(g);
	}

	private static int RADIUS = 12; // 25
	private static final int MARGIN = -1;
	private static final int HOURS = 12;
	private static final int MINUTES = 60;
	private static final int LEN_OF_HOUR = 5;// 10
	private static final int LEN_OF_MINUTE = 3;// 5
	private static final int lineWidth = 2;

	private void test(Graphics graphics) {
		final float lineInset = 1;
		final int inset1 = (int) Math.floor(lineInset);
		final int inset2 = (int) Math.ceil(lineInset);
		final Rectangle bound = getBounds();
		final Rectangle r = Rectangle.SINGLETON.setBounds(getBounds());

		r.width = bound.width / 2 - inset1 - inset2;
		r.height = bound.height / 2 - inset1 - inset2;
		r.x = bound.x + bound.width / 4 + inset1;
		r.y = bound.y + bound.height / 4 + inset1;

		graphics.drawArc(r, (int) 0, (int) 360);
		Point downPointStart = new Point(r.x + r.width / 2, r.y + r.height);
		Point downPointEnd = new Point(r.x + r.width / 2, bound.y + bound.height);
		graphics.setLineWidth(lineWidth);
		graphics.drawLine(downPointStart, downPointEnd);
		// 点亮
		if (getValue() == true) {
			for (int i = 1; i <= MINUTES; i++) {
				int len = LEN_OF_MINUTE;
				float lineWidth = 1f;
				if (i % (MINUTES / HOURS) == 0) {
					len = LEN_OF_HOUR;
					lineWidth = 2f;
				} else {
					continue;
				}

				if (i == 1 || i == 60) {
					continue;
				}

				RADIUS = r.width / 2 + len;
				int relative = RADIUS + MARGIN;

				int offsetX = r.x + inset1 + inset2 - len;
				int offsetY = r.y + inset1 + inset2 - len;

				double angle = (2 * Math.PI / MINUTES) * i;

				// 起点和终点
				Point from = new Point((offsetX + relative + RADIUS * Math.sin(angle)), (offsetY + relative + RADIUS * Math.cos(angle)));
				Point to = new Point((offsetX + relative + (RADIUS - len) * Math.sin(angle)), (offsetY + relative + (RADIUS - len) * Math.cos(angle)));

				Polyline line = new Polyline();
				// 灯泡颜色
				line.setForegroundColor(ColorConstants.yellow);
				line.addPoint(from);
				line.addPoint(to);
				line.setLineWidthFloat(lineWidth);

				PointList pl = new PointList();
				pl.addPoint(from);
				pl.addPoint(to);
				graphics.setLineWidthFloat(lineWidth);
				graphics.setForegroundColor(ColorConstants.orange);
				graphics.drawPolyline(pl);

			}
		}

		((FixedConnectionAnchor) this.connectionAnchors.get(SimpleInput.TERMINAL_IN)).offsetH = bound.width / 2;
		((FixedConnectionAnchor) this.connectionAnchors.get(SimpleInput.TERMINAL_IN)).offsetV = 0;

		Rectangle tmp = new Rectangle(bounds.x + 2, bounds.y, bounds.width, 10);
		this.nameLabel.setBounds(tmp);

		Rectangle numRectangle = new Rectangle(bound.x + bound.width / 2, bound.y + bound.height * 5 / 8, bound.width / 2 - 2, bound.height / 2 - 2);
		this.numLabel.setBounds(numRectangle);

	}

}
